package org.example.juc.p13;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @author lou ke
 * @since 2024/2/10 20:46
 */
@Slf4j
public class InterruptThread {

    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            while (true) {

                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }

                // 如果中断的标记为true，
                // 获取线程的中断标记，并且会清除
                log.info(Thread.currentThread().isInterrupted() + "");
                if (Thread.interrupted()) {
                    log.info(Thread.currentThread().isInterrupted() + "");
                    break;
                }

                log.info("定时监控...");
            }
        });
        thread.start();

        // 只是通知线程需要中断，实际上线程不会立刻中断，只是给线程做了标记
        thread.interrupt();

    }
}
